package cn.e2win.base.entity;


import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hsqldb.lib.MD5;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import cn.e2win.biz.dbo.BaseEmp;

@Entity
@Table(name = "sys_user")
public class SysUser extends BaseEntity implements UserDetails{
	private static final long serialVersionUID = 1L;
	@Id
	@Basic(optional = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", nullable = false)
	private Integer id;
	
	@Transient
	private String loginFrom;
	private String telNo;
	
	private String password;
	
	private Integer levelInt;

	private String userName;
	private String userType;
	private Integer orgId;
	private Integer empId;
	private String isLeader;

	
	private String roleDesc;
	
	private Integer numPerPage=15;
	
	private String account;
	//openId
	private String openId = "";
	//昵称
	private String nickName = "";
	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getHeadImg() {
		return headImg;
	}

	public void setHeadImg(String headImg) {
		this.headImg = headImg;
	}

	private String headImg = "";
	
	
	
	@Transient
	private Collection<? extends GrantedAuthority> authorities;
	
	
/*	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name="sys_user_role",
			joinColumns=@JoinColumn(name="userId",referencedColumnName="id"),
			inverseJoinColumns=@JoinColumn(name="roleId",referencedColumnName="id"))
	private List<SysRole> roles = new ArrayList<SysRole>();*/


	public SysUser(){
		super();
	}
	
	public SysUser(SysCompany company) {
		super.setCoId(company.getId());
		this.telNo=company.getCellPhone();
		this.account = company.getAccountPrefix() + "01";
		this.password = "123456";
		this.userName = company.getContact();
		this.levelInt = 1;
		
	}

	
	public SysUser(BaseEmp emp) {
		this.empId = emp.getId();
		this.orgId = emp.getOrgId();
		this.isLeader = emp.getIsLeader();
		this.userName = emp.getEmpName();
		this.userType = emp.getClass().getSimpleName();
		this.account = emp.getMobile();
		this.telNo = emp.getMobile();
		this.password = "123456";
	}
	
	
	
	public SysUser(SysUser user,
			boolean accountNonExpired,boolean accountNonLocked,boolean credentialsNonExpired,boolean enabled,Collection<? extends GrantedAuthority> authoritys){

		this.id = user.getId();
		this.telNo = user.getTelNo();
		this.password = user.getPassword();
		this.levelInt = user.getLevelInt();
		this.userName = user.getUserName();
		this.roleDesc = user.getRoleDesc();
		this.numPerPage = user.getNumPerPage();
		this.account = user.getAccount();
		
		this.authorities = authoritys;
		this.accountNonExpired = accountNonExpired;
		this.accountNonLocked = accountNonLocked;
		this.credentialsNonExpired = credentialsNonExpired;
		this.enabled = enabled;
	}
	

	

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLoginFrom() {
		return loginFrom;
	}

	public void setLoginFrom(String loginFrom) {
		this.loginFrom = loginFrom;
	}

	public String getTelNo() {
		return telNo;
	}

	public void setTelNo(String telNo) {
		this.telNo = telNo;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
	
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public Integer getOrgId() {
		return orgId;
	}

	public void setOrgId(Integer orgId) {
		this.orgId = orgId;
	}

	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}
	
	public String getIsLeader() {
		return isLeader;
	}

	public void setIsLeader(String isLeader) {
		this.isLeader = isLeader;
	}

	public Integer getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(Integer numPerPage) {
		this.numPerPage = numPerPage;
	}
	public String getRoleDesc() {
		return roleDesc;
	}

	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	public Integer getLevelInt() {
		return levelInt;
	}

	public void setLevelInt(Integer levelInt) {
		this.levelInt = levelInt;
	}
	public String getAccount() {
		return account;
	}
	public String getNo() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}


/*
	public List<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}
*/

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	
	@Override
	public String getUsername() {
		return this.userName;
	}

	@Transient
	private boolean accountNonExpired;
	@Override
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}
	
	@Transient
	private boolean accountNonLocked;
	@Override
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	@Transient
	private boolean credentialsNonExpired;
	@Override
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}
	
	@Transient
	private boolean enabled;
	@Override
	public boolean isEnabled() {
		return enabled;
	}

	

	
	
	
}
